define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'tongji.statistics_agent/index' + location.search,
                    add_url: 'tongji.statistics_agent/add',
                    edit_url: 'tongji.statistics_agent/edit',
                    del_url: 'tongji.statistics_agent/del',
                    multi_url: 'tongji.statistics_agent/multi',
                    table: 'statistics_agent',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'agent_name', title: __('Agent_name')},
                        {field: 'day_user', title: __('Day_user')},
                        {field: 'day_ip_num', title: __('Day_ip_num')},
                        {field: 'day_rechage_num', title: __('Day_rechage_num'),formatter: Controller.api.formatter.user},
                        {field: 'day_price', title: __('Day_price'), operate:'BETWEEN',formatter: Controller.api.formatter.price},
                        {field: 'date', title: __('Date')},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'agent_id', title: __('Agent_id')},
                        {field: 'p_agent_id', title: __('P_agent_id')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                //获取充值记录
                user: function (value, row, index) {
                    //这里手动构造URL
                    var url2 = "user.user_list/index?agent_id=" + row.agent_id+"&date="+row.date;
                    var a2 = '&nbsp;&nbsp;<a href="' + url2 + '" class="label label-success addtabsit" title="' + __("Search %s", row.agent_name+row.date+"推广用户") + '">' + value + '</a>';
                    return  a2 ;
                },
                price: function (value, row, index) {
                    //这里手动构造URL
                    var url2 = "user.user_order_list/index?agent_id=" + row.agent_id+"&date="+row.date;
                    var a2 = '&nbsp;&nbsp;<a href="' + url2 + '" class="label label-success addtabsit" title="' + __("Search %s", row.agent_name+row.date+"推广用户充值") + '">' + value + '</a>';
                    return  a2 ;
                },
            }
            
        }
    };
    return Controller;
});